﻿using System;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Data;
using System.Data.OracleClient;
using System.Reflection;

/// <summary>
/// 基础类
/// </summary>
public class BasicPage : System.Web.UI.Page
{
    private const string _defaultPass = "123456";

    public static string DefaultPass
    {
        get { return BasicPage._defaultPass; }
    } 

    public BasicPage() { }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        if (Session["LoginUser"] == null)
        {
            if (BasicPage.CheckAgent())
            {
                Response.Redirect("~/MobileLogin.aspx");
            }
            else
            {
                Response.Redirect("~/UserLogin.aspx");
            }
        }
    }

    protected void JQueryAlert(string argMessage)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), "sysinfo", "$(function(){alert('" + argMessage + "');});", true);
    }

    protected DataSet GetPubData(string argType)
    {
        string sqlstr = "SELECT DATA_CODE,DATA_NAME FROM PUBLIC_DATA WHERE DATA_TYPE = :DATA_TYPE ORDER BY DATA_CODE";
        OracleParameter[] _params = {
                new OracleParameter(":DATA_TYPE",OracleType.VarChar,20)
            };
        _params[0].Value = argType;
        return OracleHelper.ExecuteDataSet(sqlstr, CommandType.Text, _params);
    }

    protected string GetRole()
    {
        string sqlstr = "SELECT ROLE FROM USER_INFO WHERE ULOGIN=:ULOGIN";
        OracleParameter[] _params = {
                new OracleParameter(":ULOGIN",OracleType.VarChar,20)
            };
        _params[0].Value = Session["LoginUser"].ToString();
        return OracleHelper.ExecuteScalar(sqlstr, CommandType.Text, _params).ToString();
    }

    protected string GetDep()
    {
        string sqlstr = @"SELECT T2.DATA_NAME AS DEP
                              FROM USER_INFO T1
                              LEFT JOIN PUBLIC_DATA T2
                                ON T2.DATA_TYPE = 'Dep'
                               AND T2.DATA_CODE = T1.DEP
                             WHERE T1.ULOGIN = :ULOGIN";
        OracleParameter[] _params = {
                new OracleParameter(":ULOGIN",OracleType.VarChar,20)
            };
        _params[0].Value = Session["LoginUser"].ToString();
        return OracleHelper.ExecuteScalar(sqlstr, CommandType.Text, _params).ToString();
    }

    /// <summary>
    /// 获取客户端IP
    /// </summary>
    /// <returns></returns>
    public static string GetClientIP()
    {
        string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (null == result || result == String.Empty)
        {
            result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }
        if (null == result || result == String.Empty)
        {
            result = HttpContext.Current.Request.UserHostAddress;
        }
        return result;
    }

    /// <summary>
    /// 导出EXCEL
    /// </summary>
    /// <param name="ctl"></param>
    public static void DGToExcel(System.Web.UI.Control ctl, string argEncode)
    {
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
        HttpContext.Current.Response.Charset = argEncode;
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding(argEncode);
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        ctl.Page.EnableViewState = false;
        System.IO.StringWriter tw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
        ctl.RenderControl(hw);
        HttpContext.Current.Response.Write(tw.ToString());
        HttpContext.Current.Response.End();
    }

    public static void DGToExcel(System.Web.UI.Control ctl, System.Web.UI.Control ctl2)
    {
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
        HttpContext.Current.Response.Charset = "UTF-8";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        ctl.Page.EnableViewState = false;
        ctl2.Page.EnableViewState = false;
        System.IO.StringWriter tw = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
        ctl.RenderControl(hw);
        ctl2.RenderControl(hw);
        HttpContext.Current.Response.Write(tw.ToString());
        HttpContext.Current.Response.End();
    }

    /// <summary>
    /// 过滤字符串中的非法字符串
    /// </summary>
    /// <param name="text"></param>
    /// <param name="maxLength"></param>
    /// <returns></returns>
    public static string ReplaceString(string text, int maxLength)
    {
        #region
        text = text.Trim();
        if (string.IsNullOrEmpty(text))
            return string.Empty;
        if (text.Length > maxLength)
            text = text.Substring(0, maxLength);
        text = Regex.Replace(text, "[\\s]{2,}", " "); //two or more spaces
        text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); //<br>
        text = Regex.Replace(text, "(\\*)+", " "); //&nbsp;
        text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty);
        text = text.Replace("'", "''");
        return text;
        #endregion
    }

    /// <summary> 
    /// 根据 Agent 判断是否是智能手机 
    /// </summary> 
    /// <returns></returns> 
    public static bool CheckAgent()
    {
        bool flag = false;
        string agent = HttpContext.Current.Request.UserAgent;
        string[] keywords = { "Android", "iPhone", "iPod", "iPad", "Windows Phone", "MQQBrowser" };
        if (!agent.Contains("Windows NT") || (agent.Contains("Windows NT") && agent.Contains("compatible; MSIE 9.0;")))
        {
            if (!agent.Contains("Windows NT") && !agent.Contains("Macintosh"))
            {
                foreach (string item in keywords)
                {
                    if (agent.Contains(item))
                    {
                        flag = true;
                        break;
                    }
                }
            }
        }
        return flag;
    }
}
